from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from module_admin.dao.kuaile8_dao import Kuaile8Dao
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.entity.vo.kuaile8_vo import Kuaile8PageQueryModel, Kuaile8Model, DeleteKuaile8Model

from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Kuaile8Service:
    """
    kuaile8中奖号码查询模块服务层
    """

    @classmethod
    async def get_kuaile8_list_services(
        cls, query_db: AsyncSession, query_object: Kuaile8PageQueryModel, is_page: bool = False
    ):
        """
        获取kuaile8中奖号码查询列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: kuaile8中奖号码查询列表信息对象
        """
        kuaile8_list_result = await Kuaile8Dao.get_kuaile8_list(query_db, query_object, is_page)

        return kuaile8_list_result


    @classmethod
    async def add_kuaile8_services(cls, query_db: AsyncSession, page_object: Kuaile8Model):
        """
        新增kuaile8中奖号码查询信息service

        :param query_db: orm对象
        :param page_object: 新增kuaile8中奖号码查询对象
        :return: 新增kuaile8中奖号码查询校验结果
        """
        try:
            await Kuaile8Dao.add_kuaile8_dao(query_db, page_object)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message='新增成功')
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def edit_kuaile8_services(cls, query_db: AsyncSession, page_object: Kuaile8Model):
        """
        编辑kuaile8中奖号码查询信息service

        :param query_db: orm对象
        :param page_object: 编辑kuaile8中奖号码查询对象
        :return: 编辑kuaile8中奖号码查询校验结果
        """
        edit_kuaile8 = page_object.model_dump(exclude_unset=True, exclude={})
        kuaile8_info = await cls.kuaile8_detail_services(query_db, page_object.id)
        if kuaile8_info.id:
            try:
                await Kuaile8Dao.edit_kuaile8_dao(query_db, edit_kuaile8)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='kuaile8中奖号码查询不存在')

    @classmethod
    async def delete_kuaile8_services(cls, query_db: AsyncSession, page_object: DeleteKuaile8Model):
        """
        删除kuaile8中奖号码查询信息service

        :param query_db: orm对象
        :param page_object: 删除kuaile8中奖号码查询对象
        :return: 删除kuaile8中奖号码查询校验结果
        """
        if page_object.ids:
            id_list = page_object.ids.split(',')
            try:
                for id in id_list:
                    await Kuaile8Dao.delete_kuaile8_dao(query_db, Kuaile8Model(id=id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入为空')

    @classmethod
    async def kuaile8_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取kuaile8中奖号码查询详细信息service

        :param query_db: orm对象
        :param id: 
        :return: 对应的信息
        """
        kuaile8 = await Kuaile8Dao.get_kuaile8_detail_by_id(query_db, id=id)
        if kuaile8:
            result = Kuaile8Model(**CamelCaseUtil.transform_result(kuaile8))
        else:
            result = Kuaile8Model(**dict())

        return result

    @staticmethod
    async def export_kuaile8_list_services(kuaile8_list: List):
        """
        导出kuaile8中奖号码查询信息service

        :param kuaile8_list: kuaile8中奖号码查询信息列表
        :return: kuaile8中奖号码查询信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'id': '',
            'qi': '',
            'zjNumber': '',
        }
        binary_data = ExcelUtil.export_list2excel(kuaile8_list, mapping_dict)

        return binary_data

